package com.tl.mqs.mqs;

import com.huawei.it.eip.ump.client.consumer.Consumer;
import com.huawei.it.eip.ump.client.consumer.MessageModel;
import com.huawei.it.eip.ump.common.exception.UmpException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MqsConsumerClient implements InitializingBean {

    private Logger logger = LoggerFactory.getLogger(MqsConsumerClient.class);
    @Autowired
    private MqsConfig config;
    private Consumer consumer;

    @Override
    public void afterPropertiesSet() throws Exception {
        logger.info("MQS建立链接... ...");
        this.connect();
    }

    public void  connect(){
        try {
            consumer = new Consumer();
            consumer.setUmpNamesrvUrls(config.getUrls());
            consumer.setAppId(config.getAppid());
            consumer.setAppSecret(config.getAppSecret());
            consumer.setTopic(config.getTopic());
            consumer.setEncryptTransport(false);
            consumer.setMessageModel(MessageModel.CLUSTERING);
            consumer.subscribe(new MessageCallback());
            logger.info("消费主题：{}",config.getTopic());
            consumer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
